<?php
/**
 * 
 * Customer Seach Result Grid
 * 
 */

use Fox\MaterialManagement\Material\Hierarchy;
use Fox\MaterialManagement\Material\Type;
use Fox\Generator;
use Fox\Html\AnchorObject;


//
$typeLabel = array();
$finder = new Type();
$types = $finder->find(new Fox\Dao\SqlFragment("language = ?",array(Xview::instance()->language())));
foreach($types as $type)
{
	$typeLabel[$type->type] = $type->label();
}
$hierarchyLabel = array();
$hiearchies = Hierarchy::loadTree();
foreach($hiearchies as $hierarchy)
{
	$parentid = $hierarchy['parentid'];
	if(!empty($parentid) && !isset($hiearchyTree[$parentid]))
		continue;

	if(empty($parentid))
	{
		$hierarchyLabel[$hierarchy['code']] = $hierarchy['label'];
		$hiearchyTree[$hierarchy['id']] =  $hierarchy['code'];
	}
	else 
	{
		$code = $hiearchyTree[$parentid];
		$code .= $hierarchy['code'];
		$hierarchyLabel[$code] = $hierarchyLabel[$hiearchyTree[$parentid]]." / ".$hierarchy['label'];
	}
}
$grid = new Fox\Grid("gResult");
$grid->fieldcat(array(array("mat_hierarchy","hierarchy"),"label",array("material_type","type"),"uname","udate"));
$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()->field("label")->width(70);
$grid->fieldcat()->field("type")->noOutput();
$grid->fieldcat()->setGenerators(array(
	'type' => Fox\Generator::useFunction(function ($data,$key) use($typeLabel){
		$material = $data->material();
		if(isset($typeLabel[$material->type]))
			return $typeLabel[$material->type];
		else 
			return $material->type;
	
	}),
	'hierarchy' => Fox\Generator::useFunction(function ($data,$key) use($hierarchyLabel){
		$material = $data->material();
		if(isset($hierarchyLabel[$material->hierarchy]))
			return $hierarchyLabel[$material->hierarchy];
		else 
			return $material->hierarchy;
	
	}),
		
	));

	
$grid->setFieldAttr(array("label"),"setAnchor",
								new AnchorObject("?mod=material&act=update",array(
					'materialid' => Fox\Generator::useAttribute())
				,"_blank"));
			
return $grid;